package com.client.ctrl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.client.service.ClientService;
import com.client.view.RoatMainFrame;
import com.entity.Message;
import com.entity.Result;
import com.entity.User;
import com.util.Command;

public class LoginCtrl implements ActionListener{

	 private ClientService clientService=new ClientService();
	
	private JButton loginButton;
	private JTextField accountField;
	private JFrame loginFrame;
	private JPasswordField passField;
	private ButtonGroup roleGroup;
	/**
	 * �����½
	 * @param loginButton
	 * @param loginFrame
	 * @param accountField
	 * @param passField
	 */
	
	public LoginCtrl(JButton loginButton, JFrame loginFrame,
			JTextField accountField, JPasswordField passField,ButtonGroup roleGroup) {
		this.loginButton=loginButton;
		this.loginFrame=loginFrame;
		this.accountField=accountField;
		this.passField=passField;
		this.roleGroup=roleGroup;
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==loginButton){
			String account=accountField.getText();
			String pass=String.valueOf(passField.getPassword());
			String role=roleGroup.getSelection().getActionCommand();
			if(account==null && pass==null){
				JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>�˺Ż������벻��Ϊ��</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
			}else{
				User user=new User(account, pass, role);
				Message message=new Message(null, Command.LOGIN, user);
				try {
					Result result=clientService.service(message);
					User reUser=(User)result.getObject();
					if(reUser.getUserId()==null){
						JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>账号不存在</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
					     accountField.setText("");
					}else if(reUser.getPass()==null){
						JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>密码错误</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
					    passField.setText("");
					}else if(reUser.getRole()==null){
						JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>您身份不正确</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='green'>"+reUser.getUserId()+"成功登录中信教务系统</font></h></html>"), null, JOptionPane.OK_OPTION);
						loginFrame.setVisible(false);
						new RoatMainFrame(reUser).setVisible(true);
					}
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}
		
	}

}
